//
//  UplodaImageWithGongSi.m
//  YellowShopProject
//
//  Created by ypj on 2021/6/24.
//

#import "UplodaImageWithGongSi.h"
@interface UplodaImageWithGongSi ()
@property(nonatomic,assign)CGFloat itemW;
@property(nonatomic,assign)CGFloat itemH;
@end
@implementation UplodaImageWithGongSi
- (void)awakeFromNib {
    [super awakeFromNib];
    self.locationArray=[NSMutableArray array];
    self.xingLb.hidden=YES;
    self.itemW = floor((ScreenWidth - 32.0)/3.0);
    self.itemH = floor((ScreenWidth-32.0)/3.0);
    self.collectionView.showsVerticalScrollIndicator=NO;
    self.collectionView.showsHorizontalScrollIndicator=NO;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(self.itemW, self.itemH);
    _collectionView.collectionViewLayout = layout;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [_collectionView registerClass:[MenDianImageCollectionViewCell class] forCellWithReuseIdentifier:[MenDianImageCollectionViewCell reuseIdentifier]];
    [_collectionView registerClass:[MD_CollectionHeaderView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
}
+ (instancetype)shareInstance{
    return [[NSBundle mainBundle] loadNibNamed:@"UplodaImageWithGongSi" owner:nil options:nil][0];
}

-(void)setDataImageArray:(NSMutableArray *)imageArray{
     self.imageArray=imageArray;
    [self getCollectionViewHeight];
    [self.collectionView reloadData];
}

#pragma mark----delegate、dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MenDianImageCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:kClsID(MenDianImageCollectionViewCell) forIndexPath:indexPath];
    if (self.imageArray.count - 1 == indexPath.row) {
        NSString *imageString = self.imageArray[indexPath.row];
        cell.imageContent.image=[UIImage imageNamed:imageString];
        cell.delectButton.hidden=YES;
    } else {
        if ([kUserDefaultForKey(EduitString_key) isEqualToString:EduitString]) {
            cell.delectButton.hidden=NO;
        }
        else {
            cell.delectButton.hidden=YES;
        }
        NSString  *  url=self.imageArray[indexPath.row];
        [cell.imageContent sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,[NSString urlEncoding:url]]] placeholderImage:[UIImage imageNamed:@"default_icon"]];
        cell.delectButton.tag = indexPath.row;
       [cell.delectButton addTarget:self action:@selector(deleFraomArray:) forControlEvents:UIControlEventTouchUpInside];

    }
      return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        MD_CollectionHeaderView * headerV = (MD_CollectionHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
        return headerV;
        
        
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

        UICollectionReusableView  * footerView=(UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collettionSectionFoot" forIndexPath:indexPath];
        return footerView;
        
    } else {
        return nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat  photoWidth=(ScreenWidth-32.0) / 3.0;
    CGSize size=CGSizeMake(photoWidth, photoWidth);
    return size;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenWidth, 35);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.imageArray.count-1==indexPath.row) {
        self.ToShowAlert();
   
      } else {
           MenDianImageCollectionViewCell *cell = (MenDianImageCollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
          self.ToShowImage(cell.imageContent.image);
    }
}
#pragma mark- 照片点击红色按钮删除 、collection高度
-(void)deleFraomArray:(UIButton *)button
{
    self.ToDelectImage(button.tag);
}

-(void)delectImageSure:(NSInteger)tag{
    [self.imageArray removeObjectAtIndex:tag];
     self.ToDelectFileUrl(tag);
    [self getCollectionViewHeight];
    [self.collectionView reloadData];
}

-(void)getCollectionViewHeight{

    CGFloat photoWidth = (ScreenWidth - 32.0)/3.0;
    _collectionViewHeight=0;
    _collectionViewHeight=_collectionViewHeight + 35;
    NSInteger   number= self.imageArray.count % 3;
    NSInteger   zomeNumber=self.imageArray.count / 3;
    if (number== 0){
        number=zomeNumber;
    } else {
        number=zomeNumber+1;
    }
    _collectionViewHeight=_collectionViewHeight+(number*photoWidth);
    _collectionView.height=_collectionViewHeight;
    _bgView.height=_collectionViewHeight;
    [_collectionView reloadData];
}


@end
#pragma mark----图片collectionCell;
@implementation  MenDianImageCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self onInitUI];
    }
    return self;
}
-(void)onInitUI{
    CGFloat  photoWidth=(ScreenWidth-32.0)/3.0;

    self.backgroundColor=[UIColor whiteColor];
    self.imageContent=[[UIImageView   alloc]init];
    self.imageContent.frame=CGRectMake(10, 10, photoWidth-20,  photoWidth-20);
    self.imageContent.contentMode=UIViewContentModeScaleToFill;
    [self.imageContent createBordersWithColor:nil withCornerRadius:6.0 andWidth:0];
    [self addSubview:self.imageContent];
    
    self.delectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.delectButton.frame = CGRectMake(self.imageContent.right-20, self.imageContent.top, 20, 20);
    self.delectButton.hidden = NO;
    self.delectButton.userInteractionEnabled = YES;
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    img.image = [UIImage imageNamed:@"delect_icon"];
    [self.delectButton addSubview:img];
    [self addSubview:_delectButton];

}
+ (NSString*)reuseIdentifier{
    
    return @"MenDianImageCollectionViewCell";
    
}
@end
#pragma marrk----自定义头部信息
@implementation MD_CollectionHeaderView
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self){
        self.backgroundColor=[UIColor whiteColor];
        self.titleNameLb=[[UILabel alloc] initWithFrame:CGRectMake(10, 8, ScreenWidth-30, 18)];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"公司描述图(必传)"];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"公司描述图(必传)" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Medium" size: 15], NSForegroundColorAttributeName: [UIColor blackColor]}];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, str.length-5)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(5, str.length-5)];
        self.titleNameLb.attributedText=string;
        self.titleNameLb.attributedText=str;
       [self addSubview:self.titleNameLb];
    }
    return self;
}

@end
